/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxMSM_InfiniteGroundDetectionHandler.h>
#include <sxHeightCollisionEvent.h>

const static float DEFAULT_GROUND_HEIGHT = 0.0f;

Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::MSM_InfiniteGroundDetectionHandler() {
	this->groundHeight = DEFAULT_GROUND_HEIGHT;
}

Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::MSM_InfiniteGroundDetectionHandler(float groundHeight) {
	this->groundHeight = groundHeight;
}

Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::~MSM_InfiniteGroundDetectionHandler() {}

bool Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::construct(Util::ArrayList<std::shared_ptr<Objects::MassSpringMesh> >& objects) {
	return true;
}

bool Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::update(Util::ArrayList<std::shared_ptr<Objects::MassSpringMesh> >& objects, InfiniteGroundCollisionEventAggregate<Objects::MassSpringMesh>* const collisionData) {
	return true;
}

bool Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::performDetection(Util::ArrayList<std::shared_ptr<Objects::MassSpringMesh> >& objects, InfiniteGroundCollisionEventAggregate<Objects::MassSpringMesh>* const collisionData) {
	//--------------------------------------------------------------------------
	// Iterate through the objects and determine which vertices are below the
	// ground height. If the vertex is below ground height, then add a
	// collision event for that vertex.
	//--------------------------------------------------------------------------
	for ( unsigned int i = 0; i < objects.size(); i++ ) {
		std::shared_ptr<Objects::MassSpringMesh>& curMesh = objects[i];
		if ( curMesh == nullptr ) {
			std::cerr << "[MSM_InfiniteGroundDetectionHandler:performDetection] Error: Nullptr mesh encountered." << std::endl;
			std::cerr << "  Aborting collision detection." << std::endl;
			return false;
		}

		for ( unsigned int x = 0; x < curMesh->getNumNodes(); x++ ) {
			if ( curMesh->getNodePosition(x).y() < this->groundHeight ) {
				//--------------------------------------------------------------
				// Retrieve a new free index from the collision pool and then
				// set the data of that index to the newly created event.
				//--------------------------------------------------------------
				unsigned int eIndex = collisionData->eventpool_VertexFace.getFreeEventIndex();
				collisionData->eventpool_VertexFace[eIndex] = Physics::Collision::Event::Events::HeightCollisionEvent<Objects::MassSpringMesh>(curMesh, x, this->groundHeight);
			}
		}
	}

	return true;
}

void Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::setGroundHeight(float groundHeight) {
	this->groundHeight = groundHeight;
}

float Sx::Simulation::Collision::Handlers::Detection::MSM_InfiniteGroundDetectionHandler::getGroundHeight() const {
	return this->groundHeight;
}
